package com.two.service.collect.controller;


import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.two.common.entity.*;
import com.two.common.util.CommonResult;
import com.two.service.collect.feign.CaiPinFeign;
import com.two.service.collect.feign.TongJiFeign;
import com.two.service.collect.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/collect/collect")
public class CollectController {

    @Autowired
    private CollectService collectService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private CaiPinFeign caiPinFeign;

    @Autowired
    private TongJiFeign tongJiFeign;

    // 添加数据到收藏表
    @RequestMapping("selectInsert")
    public CommonResult selectInsert(@RequestBody Collect collect){
        int insert = collectService.insertAll(collect);
        if (insert==1){
            return new CommonResult(2000,"收藏成功",null);
        }
        return new CommonResult(5000,"收藏失败,可能已添加过购物车",null);
    }

    // 后台页面查询收藏表中的内容
    @RequestMapping("selectAll")
    public CommonResult selectAll(@RequestParam Integer currentPage,@RequestParam  Integer pageSize,@RequestParam  String token, @RequestBody CollectVo collectVo){
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Gson gson = new Gson();
        UserAndPermissions userAndPermissions = gson.fromJson(s, UserAndPermissions.class);
        User user = userAndPermissions.getUser();
        Integer userid = user.getUserid();
        PageInfo<Menu> list = caiPinFeign.selectAll(currentPage, pageSize, userid, collectVo);
        return new CommonResult(2000,"查询成功",list);
    }

    // 手机页面查询收藏表中的内容
    @RequestMapping("CollectSelect")
    public CommonResult CollectSelect(HttpServletRequest request){
        String token = request.getHeader("token");
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        String s = operations.get(token);
        Gson gson = new Gson();
        UserAndPermissions userAndPermissions = gson.fromJson(s, UserAndPermissions.class);
        User user = userAndPermissions.getUser();
        Integer userid = user.getUserid();
        List<Menu> list = caiPinFeign.CollectSelect(userid);
        return new CommonResult(2000,"查询成功",list);
    }

    // 手机端取消收藏
    @RequestMapping("delectCollect")
    public CommonResult delectCollect(Integer mid){
        Integer integer = collectService.delectCollect(mid);
        if (integer==1){
            return new CommonResult(2000,"取消收藏成功",null);
        }
        return new CommonResult(5000,"取消收藏失败",null);
    }

    // 查询退款信息
    @RequestMapping("tuimoney")
    public CommonResult selectAll(String token){
        ValueOperations<String, String> operations = redisTemplate.opsForValue();
        String s = operations.get(token);
        Gson gson = new Gson();
        UserAndPermissions userAndPermissions = gson.fromJson(s, UserAndPermissions.class);
        User user = userAndPermissions.getUser();
        String usermonicker = user.getUsermonicker();

        return collectService.selectAll(usermonicker);
    }

    // 查询统计信息
//    @PostMapping("selectTongJi")
//    public CommonResult selectTongJi(String token){
//        return tongJiFeign.findMoney(token);
//    }
    @PostMapping("selectTongJi")
    public CommonResult selectTongJi(String token){
        return tongJiFeign.findMoney(token);
    }
}


